package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.cartoon.CartoonListBean;
import com.taptech.doufu.ui.view.topicview.CartoonBigImageView;
import com.taptech.doufu.ui.view.topicview.CartoonGridImageView;
import com.taptech.doufu.ui.view.topicview.CartoonTwoImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 漫画列表适配器
 */
public class CartoonBeanAdapter extends BaseAdapter {
    private static final int CART_COUNT = 4; //卡片个数

    private Context context;
    private List<CartoonListBean> contentList;

    public CartoonBeanAdapter(Context context, List<CartoonListBean> contentList) {
        this.context = context;
        if (contentList != null) {
            this.contentList = contentList;
        } else {
            this.contentList = new ArrayList<>();
        }
    }

    public void refrushList(List<CartoonListBean> contentList){
        if (contentList != null) {
            this.contentList = contentList;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public CartoonListBean getItem(int position) {
        return contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return CART_COUNT;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        CartoonListBean model = getItem(position);
        if (model == null) {
            return convertView;
        }
        if (model.getModel() == 1) {
            if (convertView == null){
                convertView = new CartoonTwoImageView(context,model);
                convertView.setTag(model.getTitle());
            }else{
                if (convertView.getTag()!=null && convertView.getTag().toString().equals(model.getTitle())){
                }else{
                    convertView = new CartoonTwoImageView(context,model);
                    convertView.setTag((model).getTitle());
                }
            }
        }else if (model.getModel() == 24) {  //大图
            if (convertView == null){
                convertView = new CartoonBigImageView(context,model);
                convertView.setTag(model.getTitle());
            }else{
                if (convertView.getTag()!=null && convertView.getTag().toString().equals(model.getTitle())){
                }else{
                    convertView = new CartoonBigImageView(context,model);
                    convertView.setTag((model).getTitle());
                }
            }
        } else{

            if (convertView == null){
                convertView = new CartoonGridImageView(context,model);
                convertView.setTag(model.getTitle());
            }else{
                if (convertView.getTag()!=null && convertView.getTag().toString().equals(model.getTitle())){
                }else{
                    convertView = new CartoonGridImageView(context,model);
                    convertView.setTag((model).getTitle());
                }
            }
        }
        return convertView;
    }





    class ViewHolder {
        protected RelativeLayout layoutGround;
        protected SimpleDraweeView coverImg;
        protected TextView cartoonTitle;
        protected TextView cartoonEpisodeCount;
        protected TextView cartoonDes;
        protected ImageView userIcon;
        protected TextView cartoonUserName;
        protected TextView cartoonScanCount;
    }

    private void changeModel(ViewHolder viewHolder){
      if(WeMediaApplication.getInstance().isDayNightMode_Night){
          changeDarkModel(viewHolder);
      }else {
          changeDayModel(viewHolder);
      }
    }
    private void changeDayModel(ViewHolder viewHolder){
        Resources resources = WeMediaApplication.getInstance().getResources();
        viewHolder.coverImg.setAlpha(Constant.imagViewAlpha);
        viewHolder.cartoonTitle.setTextColor(resources.getColor(R.color.text_color_12));
        viewHolder.cartoonEpisodeCount .setTextColor(resources.getColor(R.color.text_color_23));
        viewHolder.cartoonDes .setTextColor(resources.getColor(R.color.text_color_7));
        viewHolder.userIcon.setAlpha(Constant.imagViewAlpha);
        viewHolder.cartoonUserName .setTextColor(resources.getColor(R.color.text_color_7));
        viewHolder.cartoonScanCount .setTextColor(resources.getColor(R.color.text_color_7));
        viewHolder.layoutGround.setBackgroundColor(resources.getColor(R.color.all_activity_bg));

    }

    private void changeDarkModel(ViewHolder viewHolder){
        Resources resources = WeMediaApplication.getInstance().getResources();
        viewHolder.layoutGround.setBackgroundColor(resources.getColor(R.color.bg_dark));
        viewHolder.coverImg.setAlpha(1.0f);
        viewHolder.cartoonTitle.setTextColor(resources.getColor(R.color.title_text_dark));
        viewHolder.cartoonEpisodeCount .setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.cartoonDes .setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.userIcon.setAlpha(1.0f);
        viewHolder.cartoonUserName .setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.cartoonScanCount .setTextColor(resources.getColor(R.color.text_dark));
    }
}
